package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * The type Student controller.
 */
@RestController
@Slf4j
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学员列表
     *
     * @param page          the page
     * @param pageSize      the page size
     * @param name          the name
     * @param highestDegree the highest degree
     * @param classesId     the classes id
     * @param studentNumber the student number
     * @return the result
     */
    @GetMapping
    public Result<PageBean<Student>> selectStudentByPage(
            Integer page,
            Integer pageSize,
            String name,
            Integer highestDegree,
            Integer classesId,
            String studentNumber
    ) {
        PageBean<Student> studentPageBean = studentService.selectStudentByPage(page, pageSize, name, highestDegree, classesId, studentNumber);
        return Result.success(studentPageBean);
    }

    /**
     * 新增学员
     *
     * @param student the student
     * @return the result
     */
    @PostMapping
    public Result<?> addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return Result.success();
    }

    /**
     * 根据id删除学员
     *
     * @param id the id
     * @return the result
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteStudentById(@PathVariable Integer id) {
        studentService.deleteStudentById(id);
        return Result.success();
    }

    /**
     * 根据id查询信息
     *
     * @param id the id
     * @return the result
     */
    @GetMapping("/{id}")
    public Result<Student> selectStudentById(@PathVariable Integer id) {
        Student student = studentService.selectStudentById(id);
        return Result.success(student);
    }

    /**
     * 根据id更新学员信息
     *
     * @param student the student
     * @return the result
     */
    @PutMapping
    public Result<?> updateStudentById(@RequestBody Student student){
        studentService.updateStudentById(student);
        return Result.success();
    }

    /**
     * 违纪扣分
     *
     * @param student the student
     * @return the result
     */
    @PutMapping("/deductions")
    public Result<?> updateStudentForDeductions(@RequestBody Student student){
        studentService.updateStudentForDeductions(student);
        return Result.success();
    }

}
